package com.woniuxy01;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author 作者
 * @create 2025-08-11 9:59
 */
public class Test01 {
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchFieldException, NoSuchMethodException, InvocationTargetException {
        Class clazz = Class.forName("com.woniuxy01.Student");
        Student student1 = (Student) clazz.newInstance();
        System.out.println("student1:"+student1);

//        Constructor[] constructors = clazz.getConstructors();
//        for (Constructor constructor : constructors) {
//            System.out.println(constructor);
//        }

        //获取无参构造
        Constructor constructor1 = clazz.getConstructor();
        Student student2 = (Student) constructor1.newInstance();
        System.out.println("student2:"+student2);

        //获取带参构造
        Constructor constructor2 = clazz.getConstructor(Integer.class,
                String.class,String.class,Integer.class,String.class,String.class);
        Student student3 = (Student) constructor2.newInstance(1, "zs", "zs", 12, "zs", "zs");
        System.out.println(student3);

    }

    private static void test08() throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Class clazz = Class.forName("com.woniuxy01.Student");
        Student student = (Student) clazz.newInstance();
        Method method1 = clazz.getDeclaredMethod("sleep");
        System.out.println(method1);
        method1.setAccessible(true);
        method1.invoke(student);
        Method method2 = clazz.getDeclaredMethod("sleep",int.class);
        method2.setAccessible(true);
        method2.invoke(student,10);
    }

    private static void test07() throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Class clazz = Class.forName("com.woniuxy01.Student");
        Student student = (Student) clazz.newInstance();
//        student.study();
//        student.study("java");
//        student.study("sql",1);
        //无参方法
        Method method1 = clazz.getMethod("study");
        method1.invoke(student) ;
        //获取有参方法
        //public Method getMethod(String name, Class<?>... parameterTypes)
        //一个字符串类型的参数,参数类型，String的参数类型，String的Class
        Method method2 = clazz.getMethod("study",String.class);
        method2.invoke(student,"java") ;
        //一个字符串和一个整数类型的参数,
        Method method3 = clazz.getMethod("study",String.class,int.class);
        method3.invoke(student,"java",10) ;
    }

    private static void test06() throws ClassNotFoundException {
        Class clazz = Class.forName("com.woniuxy01.Student");

//        Method[] methods = clazz.getMethods();
//        for (Method method : methods) {
//            System.out.println(method);
//        }

        Method[] declaredMethods = clazz.getDeclaredMethods();
        for (Method declaredMethod : declaredMethods) {
            System.out.println(declaredMethod);
        }
    }


    private static void test05() throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchFieldException {
        Class clazz = Class.forName("com.woniuxy01.Student");
        Student student = (Student) clazz.newInstance();
        //反射可以访问私有成员
        Field fieldSname = clazz.getDeclaredField("sname");
        //暴力访问
        fieldSname.setAccessible(true);
        fieldSname.set(student, "zs");
        System.out.println(fieldSname.get(student));
    }

    private static void test04() throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchFieldException {
        Class clazz = Class.forName("com.woniuxy01.Student");
        Student student = (Student) clazz.newInstance();
        //传统方法操作属性：
//        student.email="xxxx@163.com";
//        System.out.println(student.email);
        //反射操作属性：
        //获取Email字段
        Field fieldEmail = clazz.getField("email");
        //给这个字段赋值(对象，值)
        fieldEmail.set(student, "xxxx@163.com");
        //获取字段的属性值
        System.out.println(fieldEmail.get(student));
    }

    private static void test03() throws ClassNotFoundException, NoSuchFieldException {
        //clazz:就是Student.class文件
        Class clazz = Class.forName("com.woniuxy01.Student");
        System.out.println(clazz.getName());
//        getFields():获取所有公有属性
        System.out.println("获取所有公有属性");
        Field[] fields = clazz.getFields();
        for (Field field : fields) {
            System.out.println(field);
            System.out.println(field.getName());
        }
        //getDeclaredFields():获取所有属性
        System.out.println("获取所有属性");
        Field[] declaredFields = clazz.getDeclaredFields();
        for (Field field : declaredFields) {
            System.out.println(field);
            System.out.println(field.getName());
        }
        System.out.println("获取一个公有字段email");
        Field fieldEmail = clazz.getField("email");
        System.out.println(fieldEmail);
        System.out.println("获取一个任意访问修饰符字段sname");
        Field fieldSname = clazz.getDeclaredField("sname");
        System.out.println(fieldSname);
    }

    private static void test02() throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        //灵活，可以扩展，字符串的值，可以写入文件
        Class clazz = Class.forName("com.woniuxy01.Student");
        //反射创建对象
        Student student = (Student) clazz.newInstance();
        System.out.println(student);

        Student student1 = new Student();
        System.out.println(student1);
    }

    private static void test01() throws ClassNotFoundException {
        //如何创建一个Class类型的对象
        Student stu = new Student();
        //方法1：类.class
        Class clazz1 = Student.class;
        //方法2：对象.getClass();
        Class clazz2 = stu.getClass();
        //方法3： Class.forName("全限定名");
        Class clazz3 = Class.forName("com.woniuxy01.Student");
        System.out.println(clazz1 == clazz2);
        System.out.println(clazz1 == clazz3);
        System.out.println(clazz2 == clazz3);
    }
}
